<?php

// chdir(dirname(__FILE__)); // 175.8.177.5
error_reporting(E_ERROR);
// require_once ("../../config.php"); // 引入配置
// require_once ("../../log.php");
// require_once ("../api_tool.php");
include "../sdk_taobao/TopSdk.php";

/**
 * @name Api接口demo（php版）
 * @desc 本接口列表，只是用于系统处理淘宝联盟的订单
 */
define("appkey","28316768");
define("secret","9e19a0e10e0077d295a12cee8e3c4798");
define("session","610281042bbfb8878e1bff4f97f96d7bf09c795b00f120d2207473775157");
define("refresh_token","610241019795bcb18b18caf53831b310b9c4d918e22bd272207473775157");
define("apikey","dwarXqtmkW");	// 其他平台给的密钥

// 淘宝客-公用-商品关联推荐
function getTaobaoTbkItemInfo($numIid) {
	$c = new TopClient;
	$c->appkey = appkey;
	$c->secretKey = secret;
	$req = new TbkItemInfoGetRequest;
	$req->setNumIids($numIid);
	$resp = $c->execute($req);
	return object_to_array($resp);
}

// 淘抢购api
function getTaobaoTbkJuTqg($adzoneId) {
	$c = new TopClient;
	$c->appkey = appkey;
	$c->secretKey = secret;
	$req = new TbkJuTqgGetRequest;
	$req->setAdzoneId($adzoneId);
	$req->setFields("click_url,pic_url,reserve_price,zk_final_price,total_amount,sold_num,title,category_name,start_time,end_time");
	$req->setStartTime("2020-04-09 09:00:00");
	$req->setEndTime("2020-08-09 16:00:00");
	$resp = $c->execute($req);
	return object_to_array($resp);
}

// 淘宝客-推广者-物料精选
function getTaobaoTbkContent($adzoneId, $cid) {
	$c = new TopClient;
	$c->appkey = appkey;
	$c->secretKey = secret;
	$req = new TbkContentGetRequest;
	$req->setAdzoneId($adzoneId);
	$req->setType("1");
	$req->setCount("100");
	$req->setCid($cid);
	$req->setContentSet("1");
	$resp = $c->execute($req);
	return object_to_array($resp);
}

// 淘宝客-推广者-物料精选
// function getTaobaoTbkDgOptimusMaterial($adzoneId, $materialId, $item_id = NULL) {
// 	$c = new TopClient;
// 	$c->appkey = appkey;
// 	$c->secretKey = secret;
// 	$req = new TbkDgOptimusMaterialRequest;
// 	$req->setPageSize("100");
// 	$req->setAdzoneId($adzoneId);
// 	$req->setMaterialId($materialId);
// 	$resp = $c->execute($req);
// 	// return $resp;
// 	return object_to_array($resp);
// }

// 淘宝客-推广者-物料搜索 
function getTaobaoTbkDgOptimusMaterial($cat, $q = '', $pageNo = 1, $pageSize = 30, $adzoneId = '110018500061', $materialId = '17004') {
	$c = new TopClient;
    $c->appkey = appkey;
    $c->secretKey = secret;
    $req = new TbkDgMaterialOptionalRequest;
    $req->setAdzoneId($adzoneId);
    $req->setMaterialId($materialId);
    $req->setCat($cat);
    $req->setQ($q);
    $req->setPageNo($pageNo);
    $req->setPageSize($pageSize);
    $resp = $c->execute($req);
    return object_to_array($resp);
}

// 淘宝客-推广者-物料精选
function taobaoTbkDgOptimusMaterial($adzoneId, $materialId, $item_id = NULL) {
	$c = new TopClient;
	$c->appkey = appkey;
	$c->secretKey = secret;
	$req = new TbkDgOptimusMaterialRequest;
	$req->setPageSize("100");
	$req->setAdzoneId($adzoneId);
	$req->setMaterialId($materialId);
	$resp = $c->execute($req);

	// return $resp;
	return object_to_array($resp);
}

// 淘宝客-推广者-物料搜索 
function getTaobaoTbkDgMaterialOptional($adzoneId, $materialId) {
	$c = new TopClient;
	$c->appkey = appkey;
	$c->secretKey = secret;
	$req = new TbkDgMaterialOptionalRequest;
	$req->setAdzoneId($adzoneId);
	$req->setMaterialId($materialId);
	$req->setQ("女装");
	$resp = $c->execute($req);

	return object_to_array($resp);
}

// 淘宝客-服务商-单品券高效转链 暂时没这个转换功能
function getTaobaoTbkPrivilege($itemId, $adzoneId, $siteId) {
	$c = new TopClient;
	$c->appkey = appkey;
	$c->secretKey = secret;
	$req = new TbkPrivilegeGetRequest;
	$req->setItemId($itemId);
	$req->setAdzoneId($adzoneId);
	$req->setSiteId($siteId);
	$resp = $c->execute($req, session);

	return object_to_array($resp);
}

// 获取个人高佣金接口
function getTaokoulingTbkPrivilege($itemId, $adzoneId, $siteId, $uid, $relation_id) {
	$url = "https://api.taokouling.com/tkl/TbkPrivilegeGet?".
					"&apikey=".apikey.
					"&itemid=".$itemId.
					"&siteid=".$siteId.
					"&adzoneid=".$adzoneId.
					"&uid=".$uid.
					"&relation_id=".$relation_id;
    $res = file_get_contents($url);
	$resp = json_decode($res);

    return object_to_array($resp);
}

// 淘宝客-公用-淘口令生成 
function createTaobaoTbkTpwd($userId, $text, $url) {
	$c = new TopClient;
	$c->appkey = appkey;
	$c->secretKey = secret;
	$req = new TbkTpwdCreateRequest;
	$req->setUserId($userId);
	$req->setText($userId);
	$req->setUrl($url);
	$req->setExt("{}");
	$resp = $c->execute($req);

	return object_to_array($resp);
}

// 淘口令解析
function createTaokoulingTblToUrl($tkl) {
	$url = "https://api.taokouling.com/tkl/tkljm?".
					"&apikey=".apikey.
					"&tkl=".$tkl;
    $res = file_get_contents($url);
	$resp = json_decode($res);

    return object_to_array($resp);
}

// 根据公用地址获得商品ID
function getTaobaoTbkItemClickExtract($url) {
	$c = new TopClient;
	$c->appkey = appkey;
	$c->secretKey = secret;
	$req = new TbkItemClickExtractRequest;
	$req->setClickUrl($url);
	$resp = $c->execute($req);

	return object_to_array($resp);
}

var_dump(getTaobaoTbkItemInfo(589393870931));
?>